package ch.ethz.inf.shop.model.simpleDatabase.operators.tests;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import ch.ethz.inf.shop.model.simpleDatabase.operators.GroupBy;
import ch.ethz.inf.shop.model.simpleDatabase.operators.GroupBy.AggregateFunction;
import ch.ethz.inf.shop.model.simpleDatabase.operators.LeftJoin;
import ch.ethz.inf.shop.model.simpleDatabase.operators.Scan;

public class TestLeftJoin {

	@Test
	public void TestProductsWithLikes() {
		Map<Integer, Integer> expectedTestResults = new HashMap<Integer, Integer>();
		expectedTestResults.put(1, 1);
		expectedTestResults.put(2, 1);
		expectedTestResults.put(12, 2);
		expectedTestResults.put(3, 1);
		Scan scan = new Scan(AllTests.ROOT_DIRECTORY + "/like.txt", "like");
		GroupBy groupBy = new GroupBy(scan, AggregateFunction.COUNT, "like.user_id", "like_cnt", "like.product_id");

		scan = new Scan(AllTests.ROOT_DIRECTORY + "/product.txt", "product");
		LeftJoin j = new LeftJoin(scan, "product.product_id", groupBy, "like.product_id");

		int cnt = 0;
		while (j.moveNext()) {
			int currentId = j.current().getInt("product.product_id");
			if (expectedTestResults.containsKey(currentId)) {
				assertEquals((int) expectedTestResults.get(currentId), j.current().getInt("like_cnt"));
			} else {
				assertEquals(-1, j.current().getInt("like_cnt"));
			}
			cnt++;
		}
		assertEquals(12, cnt);
	}

}
